/*
{
"状态":1,
"名称":"钉钉登录-判断密码",
"作者":"马学兵",
"创建时间":"2018-07-26",
"功能":"钉钉登录-判断密码",
"log": [
	{"date":"" , "dev" :"" , "content" : ""}
]
}
*/

declare  @token varchar(50) , @platform int, @dd_user varchar(100) 
declare @datasource  varchar(100)=''
declare @user_key int

set @platform = '{platform}'
set @dd_user = '{dd_user}'

set @token = replace(newid(),'-','') 

INSERT INTO [dbo].[y_login]([login_company_key],[login_user_key],[create_time],[update_time],[token])
select c.company_key , user_key , getdate(), getdate(), @token
from y_platform p 
inner join y_company c on p.company_key = c.company_key 
inner join y_platform_user  u on p.platform_key = u.platform_key 
where u.platform_user_id = @dd_user  and u.platform_key =  @platform

SELECT @user_key=user_key FROM dbo.y_platform_user WHERE platform_user_id=@dd_user AND platform_key=@platform

IF EXISTS(SELECT 1 FROM dbo.y_platform_user WHERE user_key=@user_key AND platform_key='501')
begin 
	set @datasource=@datasource+',ross_ei'
end

IF EXISTS(SELECT 1 FROM dbo.y_platform_user WHERE user_key=@user_key AND platform_key='503')
begin 
	set @datasource=@datasource+',ross_yue'
end

if @datasource<>''
set @datasource=stuff(@datasource,1,1,'')	

if @user_key=4
set @datasource='ross_ei,ross_yue'

select pu.user_key,  u.user_name  , c.company_key ,c.company_name,@datasource as datasource, @token as token
from y_platform p 
inner join y_company c on p.company_key = c.company_key 
inner join y_platform_user  pu on p.platform_key = pu.platform_key 
left join y_user u on pu.user_key = u.user_key
where pu.platform_user_id = @dd_user  and pu.platform_key =  @platform
